<?php

$basedir = preg_match("/classes/", dirname(__FILE__)) ? str_replace('classes', 'modules/storeya', dirname(__FILE__)) : dirname(__FILE__);
include_once($basedir.'/../../config/config.inc.php');
include_once($basedir.'/product-info.php');

class StoreYa extends Module
  {
  public function __construct()
    {
	$this->module_key = '3aef384e22805669016bfca72dff054b'; 
    $this->name = 'storeya';
    $this->tab = 'social_networks';
    $this->version = 1.9;
    $this->author = 'StoreYa Feed LTD.';   

    parent::__construct();

    $this->displayName = $this->l('StoreYa');
    $this->description = $this->l('StoreYa.com module is designed for automatically importing web stores to Facebook having them fully customized to fit both the Facebook arena and the brand&rsquo;s look & feel. StoreYa offers social elements and actions as well as promotional tools to help you engaging with your fans and converting them to paying customers.
<p><b>Please Install and click on the Configure link below</d></p>');
    $this->confirmUninstall = $this->l('Are you sure you want to delete the module?');    
   }
   
	public function install(){
		if (!parent::install())
			return false;

    Configuration::updateValue('CONNECTION_TOKEN', substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',5)),0,30));
    return true;
  } 
  
	public function uninstall(){
    if(!parent::uninstall())
      return false; 
    return true;
	}

  public function isKeyActive($auth_key){
    return WebserviceKeyCore::isKeyActive($auth_key);
  }

  public function getCategoryTree(){
    $this->Init();
    return (Category::getRootCategory()->recurseLiteCategTree(2, 0, $_GET['id_lang']));
  }
  
   public function getProduct(){
    $this->Init();
    return (ProductInfo::getProduct($_GET['id_lang'], $_GET['id_product']));
  }  
  
    public function getProducts(){
    $this->Init();
    return (ProductInfo::getProducts($_GET['id_lang']));
  }   
   
    public function getProductsCount(){
    $this->Init();
    return (ProductInfo::getProductsCount($_GET['id_lang']));
  }

    public function getProductsToSync(){
    $this->Init();
    return (ProductInfo::getProductsToSync($_GET['id_lang'], $_GET['daysCount']));
  }
  
  public function getProduct2(){
    $this->Init();
    return (ProductInfo::getProducts2(NULL, $_GET['id_product'], $_GET['id_lang'], NULL, NULL));
  }  

  public function getDefaultLanguage() {
    return (new Language(Configuration::get('PS_LANG_DEFAULT')));
  }

  public function getVersion(){
    return _PS_VERSION_;
  }

  public function getModuleVersion(){
    return $this->version;
  }
  
  public function getInfo()
  {
     $presta_shop_version = _PS_VERSION_;
     $storeYa_module_version = $this->version;     
     $info = "Presta Shop Version: $presta_shop_version. StoreYa Module Version: $storeYa_module_version.";
     return  $info;
 }
  
  public function toJson($results) {    
    echo json_encode($results);
  }

  public function getContent(){
		$this->showScreen();
		return $this->_html;
}

 private function showScreen()
	{
		$this->_html .=
		  '<div style="width: 500px; margin:0 auto 30px auto;">
		  <h2>'. $this->l("Congratulations on successfully installing the StoreYa module!") . '</h2>
          <h3>Please follow these steps:</h3>
          <br/>
          <h3>1. Copy the following webservice key (you will be asked to paste it during StoreYa&rsquo;s short import process). </h3>		
          <p style="text-align:center; padding:15px; width:400px; margin:auto; margin-bottom: 15px; font-size: 20px; font-weight: bold;">'. Configuration::get('CONNECTION_TOKEN'). '</p>
		  ';
	     $this->_html .= 
		 '<h3>2. Go to <a style="text-decoration:underline;" href="http://www.storeya.com/prestashop" target="_blank">www.storeya.com</a></h3>
		  <h3>3. Click on the main blue button saying: &ldquo;Get started now &mdash; Connect with Facebook&rdquo;.</h3>
		  <h3>4. Type your PrestaShop&rsquo;s URL and click on the &ldquo;Confirm&rdquo; button.</h3>
		  <h3>5. Paste the webservice key you have copied, and click on the &ldquo;Submit&rdquo; button.</h3>
		  <h3>6. Click on the &ldquo;Activate&rdquo; button.</h3>
                  <br/>
                  <h3> Stay tuned - Like us on Facebook!</h3>
                  <iframe src="https://www.facebook.com/plugins/like.php?href=http://www.facebook.com/StoreYacom&amp;show_faces=false&amp;layout=button_count&amp;width=650&amp;action=like&amp;font&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" allowTransparency="true" style="float:left;"></iframe>
          </div>';
	}
  
 private function Init()
	{
    $basedir = preg_match("/classes/", dirname(__FILE__)) ? str_replace('classes', 'modules/storeya', dirname(__FILE__)) : dirname(__FILE__);
    if(!preg_match("/^1.3.*/", _PS_VERSION_))
      include_once($basedir.'/../../init.php'); 
  }
}

$storeya = new Storeya();

if(isset($_GET['format']) && $_GET['format'] == 'json'){
  error_reporting(E_ALL); 
  ini_set("display_errors", 1); 
  header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
  header("Cache-Control: no-cache, must-revalidate" );
  header("Pragma: no-cache" );
  header("Content-type: application/json");
 

}

if(isset($_GET['method'])){    
    if(Configuration::get('CONNECTION_TOKEN') != $_GET['auth_key'])
     {
      echo -1;
      return;  
    }        
  $method = $_GET['method'];
  eval("\$results = \$storeya->\$method();");
  $storeya->toJson($results);
} 
?>